package com.zuxia.rabbit;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {


    //    @Bean
    //    public Queue groupMsgQueue() {
    //        Map<String, Object> configMap = new HashMap<>();
    //        //设定配置
    //        //死信消息交换机
    //        configMap.put("x-dead-letter-exchange", "error_ex_group");
    //        //死信消息路由键
    //        configMap.put("x-dead-letter-routing-key", "group");
    //        return new Queue("delay_order_queue_group", true, false, false, configMap);
    //    }
    //
    //    @Bean
    //    public DirectExchange getMyGroupDirectExchange(){
    //        return new DirectExchange("delay_group");
    //    }
    //
    //    @Bean
    //    public Binding getMyGroupDeaBinDing(){
    //        return BindingBuilder.bind(this.groupMsgQueue()).to(this.getMyDirectExchange()).with("group");
    //    }
    //
    //    @Bean
    //    public MessageRecoverer messageGroupRecoverer(RabbitTemplate rabbitTemplate) {
    //        // 需要配置交换机和绑定键
    //        return new RepublishMessageRecoverer(rabbitTemplate, "error_ex-group", "group");
    //    }


    @Bean
    public Queue deadMsgQueue(){
        Map<String,Object> configMap = new HashMap<>();
        //设定配置
        //消息过期时间
        configMap.put("x-message-ttl",192800000);
        //死信消息交换机
        configMap.put("x-dead-letter-exchange", "error_ex");
        //死信消息路由键
        configMap.put("x-dead-letter-routing-key", "order");
        return new Queue("dead_queue",true,false,false,configMap);
    }

    @Bean
    public DirectExchange getMyDirectExchange(){
        return new DirectExchange("delay_ex");
    }
    @Bean
    public Binding getMyDeaBinDing(){
        return BindingBuilder.bind(this.deadMsgQueue()).to(this.getMyDirectExchange()).with("order");
    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate) {
        // 需要配置交换机和绑定键
        return new RepublishMessageRecoverer(rabbitTemplate, "error_ex", "order");
    }




    @Bean
    public Queue deadMsgQueue1(){
        Map<String,Object> configMap = new HashMap<>();
        //设定配置
        //消息过期时间
        configMap.put("x-message-ttl",192800000);
        //死信消息交换机
        configMap.put("x-dead-letter-exchange", "error_ex_group");
        //死信消息路由键
        configMap.put("x-dead-letter-routing-key", "group");
        return new Queue("dead_queue_group",true,false,false,configMap);
    }

    @Bean
    public DirectExchange getMyDirectExchange1(){
        return new DirectExchange("delay_ex_group");
    }
    @Bean
    public Binding getMyDeaBinDing1(){
        return BindingBuilder.bind(this.deadMsgQueue1()).to(this.getMyDirectExchange1()).with("group");
    }

    @Bean
    public MessageRecoverer messageRecoverer1(RabbitTemplate rabbitTemplate) {
        // 需要配置交换机和绑定键
        return new RepublishMessageRecoverer(rabbitTemplate, "error_ex_group", "group");
    }

}